<html>
<?php
error_reporting (E_ALL ^ E_NOTICE);
include_once("analyticstracking.php");
?>
<script language="javascript" type="text/javascript">
<!--
var currentRow = 0;
var windSpeed = new Array();
var windDir = new Array();

function newRequest() {
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try {
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer Browsers
		try {
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				// Something went wrong
				alert("Your browser broke!");
				
				return false;
			}
		}
	}
	return ajaxRequest;
}

//Browser Support Code
function getAirportData(icao, field) {
	ajaxRequest = newRequest();	
	ajaxRequest.onreadystatechange = function() {
		if (ajaxRequest.readyState == 4) {
			// get the data from the server's response	
				data = ajaxRequest.responseText.split(":");
				//data[6] = Math.round(data[6]*100)/100;
			document.getElementById(field + 'name').innerHTML = data[2];
			document.getElementById(field + 'lat').innerHTML = Math.round(100*data[3])/100 + "&deg;";
			document.getElementById(field + 'long').innerHTML = Math.round(100*data[4])/100 + "&deg;";
			document.getElementById(field + 'elev').innerHTML = data[5] + " ft";
			document.getElementById(field + 'mag_var').innerHTML = (data[6] > 0) ? (data[6] + "&deg; E") : (-1*data[6] + "&deg; W");
			document.getElementById(field + 'tz').innerHTML = "GMT" + data[7];
			document.getElementById(field + 'metar').innerHTML = data[8]; 
		}
	}
	ajaxRequest.open("GET", "getdata.php?icao=" + icao, true);
	ajaxRequest.send();
}

function addVectors(speed1, dir1, speed2, dir2) {
		dir1 = Math.PI * dir1 / 180;
		dir2 = Math.PI * dir2 / 180;
		x = speed1 * Math.cos(dir1) + speed2 * Math.cos(dir2);
		y = speed1 * Math.sin(dir1) + speed2 * Math.sin(dir2);
		return Math.sqrt(Math.pow(x,2) + Math.pow(y,2));
}

function calcGS(ias, row) {
		
		heading = document.getElementById('mb' + row).innerHTML;
		heading = parseInt(heading);
		document.getElementById('gs' + row).innerHTML = Math.round(addVectors(windSpeed[row], windDir[row], ias, heading));
}

function getRouteData() {	
	icao1 = document.getElementById('oa').value;
	icao2 = document.getElementById('da').value;
	if (icao1 != "" && icao2 != "") {
		ajaxRequest = newRequest();
		ajaxRequest.onreadystatechange = function() {
			if (ajaxRequest.readyState == 4) {
				// get the data from the server's response	
				data = ajaxRequest.responseText.split(":");
				document.getElementById('ft' + currentRow).innerHTML = icao1 + " -> " + icao2;
				document.getElementById('d' + currentRow).innerHTML = Math.round(data[0]);
				//document.getElementById('tb' + currentRow).innerHTML = data[1] + "&deg;";
				document.getElementById('mb' + currentRow).innerHTML = data[2] + "&deg;";		
				windDir[currentRow] = parseInt(data[3]);
				windSpeed[currentRow] = parseInt(data[4]);
			}
		}
		ajaxRequest.open("GET", "getdata.php?icao1=" + icao1 + "&icao2=" + icao2, true);
		ajaxRequest.send();
	}
}

function getUpperWinds(rows, alt) {
	var _row = rows.parentElement.parentElement;
	// @todo factor out rowIndex;
	iata = document.getElementById('ft' + _row.rowIndex).innerHTML.substring(1, 4);
	if (iata != "") {
		ajaxRequest = newRequest();
		ajaxRequest.onreadystatechange = function() {
			if (ajaxRequest.readyState == 4) {
				//data = ajaxRequest.responseText.split(":");
				data = ajaxRequest.responseText;
				data = data.replace(/<(?:.|\n)*?>/gm, '');
				windDir[_row.rowIndex] = parseInt(data.substring(0, 2))*10;
				windSpeed[_row.rowIndex] = parseInt(data.substring(2, 5));
				// @todo based on actual altitude, more numbers higher up
				//document.getElementById('upperwinds').innerHTML = data;
			}	
		}
		ajaxRequest.open("GET", "getdata.php?iata=" + iata + "&alt=" + alt, true);
		ajaxRequest.send();
	}
	// @todo refactor this out of here
	calcGS(document.getElementById('ias' + _row.rowIndex).value, _row.rowIndex);
}

function removeRow(rows) {
	var _row = rows.parentElement.parentElement;	
	currentRow--;
	document.getElementById("flightPlan").deleteRow(_row.rowIndex);
}

function addRow() {
		var table = document.getElementById("flightPlan");
		var rowCount = table.rows.length;
		var row = table.insertRow(rowCount);
		
		var cellDel = row.insertCell(0);
		var buttonDel = document.createElement("input");
		buttonDel.type = "button";
		buttonDel.value = "-";
		buttonDel.id = "del";
		cellDel.appendChild(buttonDel);
		buttonDel.onclick = function() { removeRow(this); };

		var cellICAO = row.insertCell(1);
		cellICAO.innerHTML = "<div id=\"ft" + rowCount + "\"></div>";
		
		var cellMOCA = row.insertCell(2);
		var textBoxMOCA = document.createElement("input");
		textBoxMOCA.type = "text";
		textBoxMOCA.setAttribute('size', 5);
		textBoxMOCA.setAttribute('maxlength', 5);
		cellMOCA.appendChild(textBoxMOCA);

		var cellAlt = row.insertCell(3);
		var textBoxAlt = document.createElement("input");
		textBoxAlt.type = "text";
		textBoxAlt.setAttribute('size', 5);
		textBoxAlt.setAttribute('maxlength', 5);
		textBoxAlt.id = 'alt' + rowCount;
		cellAlt.appendChild(textBoxAlt);
		textBoxAlt.onchange = function() { getUpperWinds(this, this.value); };

		var cellRPM = row.insertCell(4);
		var textBoxRPM = document.createElement("input");
		textBoxRPM.type = "text";
		textBoxRPM.setAttribute('size', 4);
		textBoxRPM.setAttribute('maxlength', 4);
		textBoxRPM.id = 'rpm' + rowCount;
		cellRPM.appendChild(textBoxRPM);

		var cellIAS = row.insertCell(5);
		var textBoxIAS = document.createElement("input");
		textBoxIAS.type = "text";
		textBoxIAS.setAttribute('size', 3);
		textBoxIAS.setAttribute('maxlength', 3);
		textBoxIAS.id = 'ias' + rowCount;
		textBoxIAS.onchange = function() { calcGS(this.value, rowCount); };
		cellIAS.appendChild(textBoxIAS);

		var cellMB = row.insertCell(6);
		cellMB.innerHTML = "<div id=\"mb" + rowCount + "\"></div>";

		var cellD = row.insertCell(7);
		cellD.innerHTML = "<div id=\"d" + rowCount + "\"></div>";

		var cellGS = row.insertCell(8);
		cellGS.innerHTML = "<div id=\"gs" + rowCount + "\"></div>";

		var cellETE = row.insertCell(9);
		cellETE.innerHTML = "<div id=\"ete" + rowCount + "\"></div>";

		var cellETA = row.insertCell(10);
		cellETA.innerHTML = "<div id=\"eta" + rowCount  + "\"></div>";
		
		currentRow++;
}

-->

</script>
<?php
echo "<script language=\"javascript\" type=\"text/javascript\">\n";
if (isset($_GET['oa'])) {
	$oa = $_GET['oa'];
	echo "getAirportData('$oa', 'oa');\n";
}
if (isset($_GET['da'])) {
	$da = $_GET['da'];
	echo "getAirportData('$da', 'da');\n";
}
echo "</script>\n";
?>
<table border="1" cellspacing="1" cellpadding="2">
<tr>
<th></th>
<th>ICAO</th>
<th>Name</th>
<th>Lat</th>
<th>Long</th>
<th>Elev</th>
<th>Mag Var</th>
<th>TZ</th>
<th>METAR</th>
</tr>
<tr>
<td><font face="Arial,Helvetica,sans-serif">Origin Airport</font></td>
<td><input type="text" name ="oa" maxlength=4 size=4 id="oa" value="<?php echo $oa ?>" onChange="getAirportData(this.value, this.name)"></td>
<td><div id="oaname"></div></td>
<td><div id="oalat"></div></td>
<td><div id="oalong"></div></td>
<td><div id="oaelev"></div></td>
<td><div id="oamag_var"></div></td>
<td><div id="oatz"></div></td>
<td><div id="oametar"></div></td>
</tr>
<tr>
<td><font face="Arial,Helvetica,sans-serif">Destination Airport</font></td>
<td><input type="text" name="da" maxlength=4 size=4 id="da" value="<?php echo $da ?>" onChange="getAirportData(this.value, this.name)"></td>
<td><div id="daname"></div></td>
<td><div id="dalat"></div></td>
<td><div id="dalong"></div></td>
<td><div id="daelev"></div></td>
<td><div id="damag_var"></div></td>
<td><div id="datz"></div></td>
<td><div id="dametar"></div></td>
</tr>
</table>
<br/><br/>
<table id="flightPlan" border="1" cellspacing="1" cellpadding="2">
<tr>
<td><input type="button" name="addRow" value="+" onClick="getRouteData(); addRow()"></td>
<th>From-To</th>
<th>MOCA</th>
<th>Alt.</th>
<th>RPM</th>
<th>IAS</th>
<th>Comp.<br/>Hdg.</th>
<th>Dist.</th>
<th>G/S</th>
<th>ETE</th>
<th>ETA</th>
</tr>
<!--<tr>
<td><input type="button" name="addRow" value="+" onClick="getRouteData(); addRow('flightPlan')"></td>
<td><div id="ft1"></div></td>
<td><input type="text" name="moca1" maxlength=5 size=5></td>
<td><input type="text" name="alt1" maxlength=5 size=5></td>
<td><input type="text" name="rpm1" maxlength=4 size=4></td>
<td><input type="text" name="ias1" maxlength=3 size=3></td>
<td><div id="mb1"></div></td>
<td><div id="d1"></div></td>
<td><div id="gs1"></div></td>
<td><div id="ete1"></div></td>
<td><div id="eta1"></div></td>
</tr>-->
</table>
<div id="upperwinds"></div>
<?php

?>
</html>
